package boKe.reflect.struts;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.ConvertUtils;

public class Main {

	public static void main(String[] args) throws Exception {
		// request，获取的消息都是String类型
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("name", "zhaohui");
		paramMap.put("age", "25");
		// 加载类,实例化对象
		Object person = Class.forName("boKe.reflect.struts.Person").newInstance();
		// 处理参数
		PropertyDescriptor[] ps = Introspector.getBeanInfo(person.getClass())
				.getPropertyDescriptors();
		Object value = null;
		for (PropertyDescriptor propertyDescriptor : ps) {
			Method setter = propertyDescriptor.getWriteMethod();// 获取属性的setter方法
			if (setter != null) {
				value = ConvertUtils.convert(
						paramMap.get(propertyDescriptor.getName()),
						propertyDescriptor.getPropertyType());
				setter.invoke(person, value);
			}
		}
		System.out.println(person.toString());
	}

}
